//
//  YQCalendarEachDay.m
//  calendarDemo
//
//  Created by problemchild on 16/8/23.
//  Copyright © 2016年 ProblenChild. All rights reserved.
//

#import "YQCalendarEachDay.h"

@interface YQCalendarEachDay ()
/**
 *  显示天数号码的Lable
 */
@property(nonatomic,strong)UILabel *lab;

@property(nonatomic,strong)UIView *point;//打卡成功标记
@property(nonatomic,strong)UIView *point2;//测试成功打卡标记

@property(nonatomic,strong)UILabel *todayLabel;

@end

@implementation YQCalendarEachDay


-(UIColor *)selectedBackColor{
    return _selectedBackColor?_selectedBackColor:KThemeColor;
}
-(void)setSelectedBackColor:(UIColor *)color{
    _selectedBackColor = color;
    if(_isSelect == NO){
        self.backgroundColor = [UIColor clearColor];
    }else{
        self.backgroundColor = color;
    }
}

-(UIColor *)dayColor{
    return _dayColor?_dayColor:KFontBlackColor;
}
-(void)setDayColor:(UIColor*)color{
    _dayColor  = color;
    self.lab.textColor = color;
}

-(NSString *)dateString{
    return _dateString?_dateString:@"尚未赋值";
}
-(void)setDateString:(NSString *)dateString{
    _dateString = dateString;
}

-(instancetype)initWithFrame:(CGRect)frame andWidth:(CGFloat)currentWidth{
    
    self = [super initWithFrame:frame];
    
    self.currentAvailableWidth = currentWidth;
    [self setup];
    
    return self;
}

/**
 *  初始化
 */
-(void)setup{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = [UIColor clearColor];
    
    CGFloat lab_w = [DeviceUtils getAdaptedWidthForDesignWidth:18 andCurrentAvailableWidth:self.currentAvailableWidth];
    self.lab = [[UILabel alloc]init];
    self.lab.textAlignment = NSTextAlignmentCenter;
    self.lab.textColor = self.dayColor;
    if(lab_w < 17){
        self.lab.font = JPUIFontPingFangSCMedium(15);
    }
    [self addSubview:self.lab];
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(@0);
        make.height.mas_equalTo(lab_w);
    }];
    
    self.point = [[UIView alloc]init];
    self.point.backgroundColor = KThemeColor;
    self.point.layer.cornerRadius = 2;
    self.point.layer.masksToBounds = YES;
    [self addSubview:self.point];
    [self.point mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@4);
        make.centerX.mas_equalTo(self_weak_).offset(-3);
        make.bottom.mas_equalTo(@5);
    }];
    self.point.hidden = YES;
    
    self.point2 = [[UIView alloc]init];
    self.point2.backgroundColor = UIColorHex(0xFF781D);
    self.point2.layer.cornerRadius = 2;
    self.point2.layer.masksToBounds = YES;
    [self addSubview:self.point2];
    [self.point2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@4);
        make.centerX.mas_equalTo(self_weak_).offset(3);
        make.bottom.mas_equalTo(@5);
    }];
    self.point2.hidden = YES;
}


-(void)setTheSelectedMode:(BOOL)selected{
    _isSelect = selected;
    if(selected){
        self.backgroundColor = self.selectedBackColor;
    }else{
        self.backgroundColor = [UIColor clearColor];
    }
}

-(void)setTheDay:(int)day{
    self.lab.text = [NSString stringWithFormat:@"%d",day];
}

-(void)setIsToday:(BOOL)isToday{
    _isToday = isToday;
    if(isToday == YES){
        if(!self.todayLabel){
            self.todayLabel = [[UILabel alloc]init];
            self.todayLabel.font = JPUIFontPingFangSCMedium(15);
            self.todayLabel.textAlignment = NSTextAlignmentCenter;
            self.todayLabel.textColor = KThemeColor;
            self.todayLabel.backgroundColor = [UIColor whiteColor];
            self.todayLabel.layer.cornerRadius = 5;
            self.todayLabel.layer.borderWidth = 1.0;
            self.todayLabel.layer.masksToBounds = YES;
            self.todayLabel.layer.borderColor = KThemeColor.CGColor;
            self.todayLabel.adjustsFontSizeToFitWidth = YES;
            self.todayLabel.text = @"今";
        }
        [self addSubview:self.todayLabel];
        CGFloat t_w = [DeviceUtils getAdaptedWidthForDesignWidth:21 andCurrentAvailableWidth:self.currentAvailableWidth];
        __weak typeof(self)self_weak_ = self;
        [self.todayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(t_w);
            make.centerX.mas_equalTo(self_weak_);
            make.top.mas_equalTo(@0);
        }];
    }else{
        if(self.todayLabel){
            [self.todayLabel removeFromSuperview];
        }
    }
}

-(void)setCalendarIsLearn:(BOOL)isLearn andIsTest:(BOOL)isTest{
    __weak typeof(self)self_weak_ = self;
    if(isLearn == YES&&isTest == YES){
        self.point.hidden = NO;
        self.point2.hidden = NO;
        [self.point mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self_weak_).offset(-3);
        }];
        [self.point2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self_weak_).offset(3);
        }];
    }else if (isLearn == YES&&isTest == NO){
        self.point.hidden = NO;
        self.point2.hidden = YES;
        [self.point mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self_weak_);
        }];
    }else if (isLearn == NO&&isTest == YES){
        self.point.hidden = YES;
        self.point2.hidden = NO;
        [self.point2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self_weak_);
        }];
    }else{
        self.point.hidden = YES;
        self.point2.hidden = YES;
    }
    
}

@end
